﻿using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JanTaschenrechner
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("dwmapi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern bool DwmIsCompositionEnabled();

        [StructLayout(LayoutKind.Sequential)]
        public class MARGINS
        {
            public int cxLeftWidth, cxRightWidth,
                cyTopHeight, cyBottomHeight;
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "1";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "2";
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "3";
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "4";
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "5";
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "6";
        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "7";
        }

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "8";
        }

        private void button9_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "9";
        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "0";
        } 
        
        private void button11_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + ".";
        }

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "+";
        }

        private void butto13_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "-";
        }

        private void button14_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "*";
        }

        private void button15_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = Rechenfeld.Text + "/";
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key.ToString().Equals("NumPad1")) { Rechenfeld.Text = Rechenfeld.Text + "1"; };
            if (e.Key.ToString().Equals("NumPad2")) { Rechenfeld.Text = Rechenfeld.Text + "2"; };
            if (e.Key.ToString().Equals("NumPad3")) { Rechenfeld.Text = Rechenfeld.Text + "3"; };
            if (e.Key.ToString().Equals("NumPad4")) { Rechenfeld.Text = Rechenfeld.Text + "4"; };
            if (e.Key.ToString().Equals("NumPad5")) { Rechenfeld.Text = Rechenfeld.Text + "5"; };
            if (e.Key.ToString().Equals("NumPad6")) { Rechenfeld.Text = Rechenfeld.Text + "6"; };
            if (e.Key.ToString().Equals("NumPad7")) { Rechenfeld.Text = Rechenfeld.Text + "7"; };
            if (e.Key.ToString().Equals("NumPad8")) { Rechenfeld.Text = Rechenfeld.Text + "8"; };
            if (e.Key.ToString().Equals("NumPad9")) { Rechenfeld.Text = Rechenfeld.Text + "9"; };
            if (e.Key.ToString().Equals("NumPad0")) { Rechenfeld.Text = Rechenfeld.Text + "0"; };
            if (e.Key.ToString().Equals("D1")) { Rechenfeld.Text = Rechenfeld.Text + "1"; };
            if (e.Key.ToString().Equals("D2")) { Rechenfeld.Text = Rechenfeld.Text + "2"; };
            if (e.Key.ToString().Equals("D3")) { Rechenfeld.Text = Rechenfeld.Text + "3"; };
            if (e.Key.ToString().Equals("D4")) { Rechenfeld.Text = Rechenfeld.Text + "4"; };
            if (e.Key.ToString().Equals("D5")) { Rechenfeld.Text = Rechenfeld.Text + "5"; };
            if (e.Key.ToString().Equals("D6")) { Rechenfeld.Text = Rechenfeld.Text + "6"; };
            if (e.Key.ToString().Equals("D7")) { Rechenfeld.Text = Rechenfeld.Text + "7"; };
            if (e.Key.ToString().Equals("D8")) { Rechenfeld.Text = Rechenfeld.Text + "8"; };
            if (e.Key.ToString().Equals("D9")) { Rechenfeld.Text = Rechenfeld.Text + "9"; };
            if (e.Key.ToString().Equals("D0")) { Rechenfeld.Text = Rechenfeld.Text + "0"; };
            if (e.Key.ToString().Equals("Add")) { Rechenfeld.Text = Rechenfeld.Text + "+"; };
            if (e.Key.ToString().Equals("Subtract")) { Rechenfeld.Text = Rechenfeld.Text + "-"; };
            if (e.Key.ToString().Equals("Divide")) { Rechenfeld.Text = Rechenfeld.Text + "/"; };
            if (e.Key.ToString().Equals("Multiply")) { Rechenfeld.Text = Rechenfeld.Text + "*"; };
            if (e.Key.ToString().Equals("Decimal")) { Rechenfeld.Text = Rechenfeld.Text + "."; };
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (Environment.OSVersion.Version.Major >= 6 && DwmIsCompositionEnabled())
            {
                // Get the current window handle
                IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
                HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
                mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;

                this.Background = Brushes.Transparent;

                // Set the proper margins for the extended glass part
                MARGINS margins = new MARGINS();
                margins.cxLeftWidth = -1;
                margins.cxRightWidth = -1;
                margins.cyTopHeight = -1;
                margins.cyBottomHeight = -1;

                int result = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);

                if (result < 0)
                {
                    MessageBox.Show("Es gab einen Fehler mit dem Glas-Zeug.");
                }
            }
        }

        private void button20_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = rechnen(Rechenfeld.Text).ToString();
        }

        private Double rechnen (String eingabe) 
        {
            if (eingabe.Contains("+")){
                String[] splits = eingabe.Split(new Char[] { '+' }, 2, StringSplitOptions.RemoveEmptyEntries);
                return rechnen(splits[0]) + rechnen(splits[1]);
            }
            if (eingabe.Contains("-"))
            {
                try
                {
                    if (eingabe.Substring(eingabe.IndexOf("-") - 1, 1).Equals("1") ||
                                eingabe.Substring(eingabe.IndexOf("-") - 1, 1).Equals("2") ||
                                eingabe.Substring(eingabe.IndexOf("-") - 1, 1).Equals("3") ||
                                eingabe.Substring(eingabe.IndexOf("-") - 1, 1).Equals("4") ||
                                eingabe.Substring(eingabe.IndexOf("-") - 1, 1).Equals("5") ||
                                eingabe.Substring(eingabe.IndexOf("-") - 1, 1).Equals("6") ||
                                eingabe.Substring(eingabe.IndexOf("-") - 1, 1).Equals("7") ||
                                eingabe.Substring(eingabe.IndexOf("-") - 1, 1).Equals("8") ||
                                eingabe.Substring(eingabe.IndexOf("-") - 1, 1).Equals("9") ||
                                eingabe.Substring(eingabe.IndexOf("-") - 1, 1).Equals("0"))
                    {
                        String[] splits = eingabe.Split(new Char[] { '-' }, 2, StringSplitOptions.RemoveEmptyEntries);
                        return rechnen(splits[0]) - rechnen(splits[1]);
                    }
                }
                catch (Exception)
                {
                    //Hier tut das Programm mal einfach gar nichts!
                }
            }
            if (eingabe.Contains("*"))
            {
                String[] splits = eingabe.Split(new Char[] { '*' }, 2, StringSplitOptions.RemoveEmptyEntries);
                return rechnen(splits[0]) * rechnen(splits[1]);
            }
            if (eingabe.Contains("/"))
            {
                String[] splits = eingabe.Split(new Char[] { '/' }, 2, StringSplitOptions.RemoveEmptyEntries);
                return rechnen(splits[0]) / rechnen(splits[1]);
            }
            else {
                return ToDouble(eingabe);
            }
        }

        public static double ToDouble(string In)
        {
            In = In.Replace(",", ".");

            return double.Parse(In, System.Globalization.CultureInfo.InvariantCulture);
        }

        private void button21_Click(object sender, RoutedEventArgs e)
        {
            Rechenfeld.Text = "";
        }

    }
}
